var map; 
function locate_initialize() {    
	var mapOptions = {     
		zoom: 10,     
		center: new google.maps.LatLng(24.997344353729364, 121.55073852549200),     
		mapTypeId: google.maps.MapTypeId.ROADMAP   
	}   
	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);    
	var marker = new google.maps.Marker({
	    position: new google.maps.LatLng(24.997344353729364, 121.55073852549200),
	    draggable:true,
	    map: map,
	    title:"拖动来选择坐标"
	});
	  
    google.maps.event.addListener(marker, 'mouseup', function() {
		var position_str=marker.getPosition().toString();
		var str=position_str.replace("(","");
		str=str.replace(")","");
		var strs=str.split(",");
		lat=strs[0];
		lng=strs[1];
		document.getElementById("_latitude").value=lat;
		document.getElementById("_longitude").value=lng;
		map.setCenter(marker.getPosition());
	  });
}  
function range_initialize() {    
	var mapOptions = {          
		center: new google.maps.LatLng(24.997344353729364, 121.55073852549200),          
		zoom: 10,          
		mapTypeId: google.maps.MapTypeId.ROADMAP 
	};    
	var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
	map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById("mycontrol"));
	
	var rectangle=null;
	var click1;
	var mousemove1;
	var rectclick;
	var start=document.getElementById("start");
	var end=document.getElementById("end");
	var area;
	
	google.maps.event.addDomListener(start,"click",function(){
			start.style.fontWeight="bold";
			end.style.fontWeight="normal";
			var pointS=null;
			var pointE=null;
			click1=google.maps.event.addListener(map,"click",function(event1){
				
				mousemove1=google.maps.event.addListener(map,"mousemove",function(event2){
					 pointS=event1.latLng;
					 pointE=event2.latLng;
					 var rectOptions = {
				      strokeColor: "#FF0000",
				      strokeOpacity: 0.8,
				      strokeWeight: 2,
				      fillColor: "#FF0000",
				      fillOpacity: 0.0,
				      draggable:true,
				      editable:true,
				      map: map,
				      bounds: new google.maps.LatLngBounds(pointS,pointE)
				    };
				    if(rectangle!=null){
				    		rectangle.setMap(null);
				    		rectangle=null;
				    }
				    rectangle = new google.maps.Rectangle();
						rectangle.setOptions(rectOptions);
						rectclick=google.maps.event.addListenerOnce(rectangle,"click",function(){
									google.maps.event.removeListener(click1);	
									google.maps.event.removeListener(mousemove1);
									google.maps.event.removeListener(rectclick);
						});
				});
			});
	});
	
	google.maps.event.addDomListener(end,"click",function(){
			start.style.fontWeight="normal";
			end.style.fontWeight="bold";
			rectangle.setMap(null);
			alert("選取成功");
			area=rectangle.getBounds();
			var ne=area.getNorthEast();
			var sw=area.getSouthWest();
			$("#_latitude1").val(ne.lat());
			$("#_longitude1").val(ne.lng());
			$("#_latitude2").val(sw.lat());
			$("#_longitude2").val(sw.lng());
	});
}  
function loadScript_locate_map() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyCEqUa9YSkEVVvSy1wS5NnMrsqzo1FPpi0&sensor=false&callback=locate_initialize";
  document.body.appendChild(script);
}
function loadScript_range_map() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyCEqUa9YSkEVVvSy1wS5NnMrsqzo1FPpi0&sensor=false&callback=range_initialize";
  document.body.appendChild(script);
}
function show_locate_map(){
	loadScript_locate_map();
	$('#locate_map_select').dialog({
		autoOpen: true,
		modal: true,
		width: 650,
		height:700,
		buttons: {
			"確定": function () {
				$('#location_map1').val($('#_latitude').val());
				$('#location_map2').val($('#_longitude').val());
				$(this).dialog("close");
			},
			"取消": function () {
				$(this).dialog("close");
			}
		}
	});
}

function show_range_map(){
	loadScript_range_map();
	$('#range_map_select').dialog({
		autoOpen: true,
		modal: true,
		width: 650,
		height:780,
		buttons: {
			"確定": function () {
				$('#range_east').val($('#_latitude1').val());
				$('#range_north').val($('#_longitude1').val());
				$('#range_west').val($('#_latitude2').val());
				$('#range_south').val($('#_longitude2').val());
				$(this).dialog("close");
			},
			"取消": function () {
				$(this).dialog("close");
			}
		}
	});
}